import JSEncrypt from 'jsencrypt';

export function generateUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
        const r = (Math.random() * 16) | 0;
        const v = c === 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
}

export function encryptData(str) {
    // 创建RSA密钥对
    const encrypt = new JSEncrypt();
    encrypt.setPublicKey('-----BEGIN PUBLIC KEY-----\n' +
        'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvBGYgORCm0CM5aOuw1jR\n' +
        'SRcQz4PvbZdwvrpzdN6ENhcmU/ay+grVMMftaU+SA7eWINRp/Qib3maotWLoA3cd\n' +
        '7BxT8TW2l7JBk3U8+/5UJUngHHnv+DiEHeDnL8powJSaga7Ca2C6V1FjkbEDV7lE\n' +
        'AhrCBuigBdd3Xd6scMqqYzWWsfuK303e7Sr4guYzhrrA+5U+LdbgW054xuK0ulx/\n' +
        'V7jGhz63E5q5RrNL1TvqQ9GtXghDSE81pJc/C+GSXd58uKGdzWQu7ONzaqrtWLIc\n' +
        'C85mFk03Lo+Y3eTzSGAKyyNWQ478253H2GdPB7JmxqlJk7cuEiPcaXv//RCIQ/6o\n' +
        'AQIDAQAB\n' +
        '-----END PUBLIC KEY-----\n');

    const nonce = generateUUID(); // 生成唯一标识
    const timestamp = Date.now();
    const data = `${str}|${nonce}|${timestamp}`;
    return encrypt.encrypt(data);
}